﻿var failuretimer = 0;
var already = 0;

function AddSelectOption(selectObj, text, value, isSelected) {
    if (selectObj != null && selectObj.options != null)
    {
        selectObj.options[selectObj.options.length] = 
            new Option(text, value, false, isSelected);
    }
}

function toggleSchool(id) {
	if($('school_data_'+id).value == '1') {
		$('school_'+id).src = '../data/icons/work.png';
		$('school_data_'+id).value = '0';
	} else {
		$('school_'+id).src = '../data/icons/school.png';
		$('school_data_'+id).value = '1';
	}
	new Ajax.Request('entries.cfm', {
			method: 'get',
			parameters: {action: 'setSchool', id: id, value: $('school_data_'+id).value },
			onFailure: function() { doFailure(true); }
		});
}

function showTemplateMenu() {
	Effect.Fade('tmp1', { duration: 0.4 } );
	Effect.Appear('tmp2', { duration: 0.4 } );
	Effect.Appear('tmp3', { duration: 0.4 } );
}


function addTemplate () {
	if($('text').value != '') {
	new Ajax.Request('entries.cfm', {
		method: 'get',
		parameters: {action: 'addtmp', text: $('text').value },
		onSuccess: function(){
				$('text').value = '';
				Effect.Fade('tmp2', { duration: 0.4 } );
				Effect.Fade('tmp3', { duration: 0.4 } );
				Effect.Pulsate('addForm', { pulses: 1, duration: 0.4 });
				window.setTimeout("Effect.Appear('tmp1', { duration: 0.4 } )",400);
				already = 0;
				$('text2').length = 0;
		},
		onFailure: function() {
			doFailure(true);
		}
	});
	}
}

function deleteTemplate() {
	new Ajax.Request('entries.cfm', {
		method: 'get',
		parameters: {action: 'deletetmp', text: $('text2').value },
		onSuccess: function(){
				$('text2').length = 0;
				already = 0;
				showTemplates();
		},
		onFailure: function() {
			doFailure(true);
		}
	});
}

function showTemplates() {
	Effect.Fade('tmp2', { duration: 0.4 } );
	Effect.Fade('tmp3', { duration: 0.4 } );
	var json = '';
	var i = 0;
	if(already == 0) {
	new Ajax.Request('entries.cfm', {
		method:'get',
		parameters: {action: 'listtmp'},
		onSuccess: function(transport){
			json = transport.responseText.evalJSON();
			while (typeof(json[i]) != "undefined") {
				Effect.Fade('text', { duration: 0.4 } );
				AddSelectOption($('text2'), json[i], json[i], false);
				i++;
			}
		},
		onFailure: function() {
			doFailure(true);
		}
	});
	already = 1;
	} else {
		Effect.Fade('text', { duration: 0.4 } );
	}
	Effect.Appear('tmp4', { duration: 0.4 } );
	window.setTimeout("Effect.Appear('text2', { duration: 0.4 } )",400);
}

function doFailure(bool) {
	if($('failure').style.display != 'none') {
		if(bool == true) {
			clearTimeout ( failuretimer );
			failuretimer = setTimeout("doFailure(false)", 5000);
			Effect.Pulsate('failure', { pulses: 1, duration: 0.4 });
		} else {
			Effect.Fade('failure', { duration: 0.4 } );
		}
	} else {
		Effect.Appear('failure', { duration: 0.4 } );
		failuretimer = setTimeout("doFailure(false)", 5000);
	}
}

function ajaxDelete (id, parent) {
	var result = confirm('Wirklich diesen Eintrag löschen?');
	if( result == true ) {
		new Ajax.Request('entries.cfm', {
			method: 'get',
			parameters: {action: 'delete', id: id},
			onSuccess: function(){
				$('info_max').value = parseInt($('info_max').value, 10)-1;
				$('visible_fields_'+parent).value = parseInt($('visible_fields_'+parent).value, 10)-1;
				if(parseInt($('visible_fields_'+parent).value, 10) <= 0) {
					Effect.Fade('day_'+parent, { duration: 0.4 });
				} else
					Effect.Fade('item_'+id, { duration: 0.4 });
				},
			onFailure: function() {
				doFailure(true);
			}
		});
	}
}


function startEdit (id) {
	var text = $('entry_'+id).innerHTML;
	var childs = $('entry_'+id).childElements();
	if(childs.length == 0) {
		$('entry_'+id).innerHTML = '<input onblur="ajaxEdit(\''+id+'\');" type="name" value="'+text.replace(/"/g, '&quot;')+'" id="edit_'+id+'" class="edit" maxlength="200" onKeyUp="isEnterKey(this, event, \''+id+'\')">';
		$('edit_'+id).focus()
	}
}

function isEnterKey(fld, evt, id) {
	evt = (evt) ? evt : (window.event) ? window.event : "";

	var theKey ;
	if (evt) {
		theKey = (evt.which) ? evt.which : evt.keyCode ;
	}
	if (theKey == 13) {
		ajaxEdit(id);
		return true;
	} else
		return false ;
}

function ajaxEdit (id) {
	var text = $('edit_'+id).value;
	new Ajax.Request('entries.cfm', {
		method: 'get',
		parameters: {action: 'update', id: id, text: text},
		onSuccess: function(){ $('entry_'+id).innerHTML = text; },
		onFailure: function() { doFailure(true); }
	});
}

function ajaxAdd () {
	var form = $('addForm');
	var date = $(form['year']).getValue()+'-'+$(form['month']).getValue()+'-'+$(form['day']).getValue();
	if($('text').style.display != 'none') {
		var text = $(form['text']).getValue();
	} else {
		var text = $(form['text2']).getValue();
	}
	var old = $('info_max').value;

	new Ajax.Request('entries.cfm', {
		method: 'post',
		parameters: {action: 'add', text : text, date : date },
		onSuccess: function() {
			$('text').value = '';
			if($('text2').style.display != 'none') {
				Effect.Fade('text2', { duration: 0.001 } );
				Effect.Fade('tmp4', { duration: 0.4 } );
				Effect.Appear('tmp1', { duration: 0.4 } );
				window.setTimeout("Effect.Appear('text', { duration: 0.4 } )",1);
			}
			Effect.Pulsate('addForm', { pulses: 1, duration: 0.4 });
			new Ajax.Request('entries.cfm', {
				method: 'get',
				parameters: {action: 'countDays' },
				onSuccess: function(transport) {
					$('info_max').value = transport.responseText.evalJSON().amount;
				},
				onFailure: function() {
					doFailure(true);
				}
			});
			getNewList(old);	
		},
		onFailure: function() { doFailure(true); }
	});
	
}

function ajaxMove (string) {
	string_array = string.split("&");
	for(i = 0; i < string_array.length; i++) {
		part = string_array[i].split("=");
		new Ajax.Request('entries.cfm', {
			method: 'get',
			parameters: {action: 'move', id : part[1], position : i },
			onFailure: function() { doFailure(true); }
		});
	}
}
